<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-stock__birth}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-select v-model="dataForm.houseId" placeholder="猪舍" clearable>
            <el-option
                v-for="item in houses"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <!--<pig-tree :props="props"
            :options="optionData"
            :value="valueId"
            :filter="0"
            :clearable="isClearable"
            :accordion="isAccordion"
            @getValue="getValue($event)"></pig-tree>-->
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="addHandle()" type="success">{{ $t('add') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle"  @sort-change="dataListSortChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column v-for="field in m_ltFields" :key="field.prop" :prop="field.prop" :fixed="field.fixed" sortable="custom" :label="field.label" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <!-- <el-button v-if="$hasPermission('stock:birth:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button> -->
            <el-button type="text" size="small" @click="addHandle(scope.row)">{{ $t('update') }}</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <!--<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>-->
      <breed-birth v-if="birthVisible" ref="refBirth" @refreshData="getDataList"></breed-birth>
    </div>
  </el-card>
</template>

<script>/* eslint-disable */
import mixinViewModule from '@/mixins/view-module'
import SearchBar from './search-bar'
import PigTree from '@/views/modules/components/pig-tree'
import BreedBirth from '@/views/modules/stock/production/breed-birth'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/history/birth/page',
        getDataListIsPage: true,
        exportURL: '/history/birth/export',
        deleteURL: '/history/birth',
        deleteIsBatch: true
      },
      m_ltFields: [ { prop: 'houseName', label: '所在猪舍', fixed: 'left', readonly: true, search: true }, { prop: 'earmark', label: '母猪耳号', fixed: 'left', required: true, search: true }, { prop: 'birthDate', label: '分娩时间', required: true, search: true }, { prop: 'type', label: '类型' }, { prop: 'hours', label: '产程', type: 'number' }, { prop: 'wellTotal', label: '健仔头数', type: 'number', required: true, search: true }, { prop: 'wellWeight', label: '健仔重量', type: 'number' }, { prop: 'weakTotal', label: '弱仔头数', type: 'number', search: true }, { prop: 'weakWeight', label: '弱仔重量', type: 'number' }, { prop: 'abnormalTotal', label: '畸形头数', type: 'number' }, { prop: 'abnormalWeight', label: '畸形重量', type: 'number' }, { prop: 'deathTotal', label: '死胎头数', type: 'number', search: true }, { prop: 'mummyTotal', label: '木乃伊头数', type: 'number' }, { prop: 'operator', label: '接产员' }, { prop: 'createDate', label: '创建时间', readonly: true } ],
      searchFields: [ { prop: 'houseId', label: '所在猪舍', fixed: 'left', readonly: true, search: true }, { prop: 'earmark', label: '母猪耳号', fixed: 'left', required: true, search: true }, { prop: 'birthDate', label: '分娩时间', required: true, search: true } ],
      dataForm: {
        id: ''
      },
      birthVisible: false,
      isClearable: true, // 可清空（可选）
      isAccordion: true, // 可收起（可选）
      valueId: 1, // 初始ID（可选）
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children"
        // disabled:true
      },
      // 选项列表（必选）
      list: [
        { id: 1, parentId: 0, name: "一级菜单A", rank: 1 },
        { id: 2, parentId: 0, name: "一级菜单B", rank: 1 },
        { id: 3, parentId: 0, name: "一级菜单C", rank: 1 },
        { id: 4, parentId: 1, name: "二级菜单A-A", rank: 2 },
        { id: 5, parentId: 1, name: "二级菜单A-B", rank: 2 },
        { id: 6, parentId: 2, name: "二级菜单B-A", rank: 2 },
        { id: 7, parentId: 4, name: "三级菜单A-A-A", rank: 3 },
        { id: 8, parentId: 7, name: "四级菜单A-A-A-A", rank: 4 },
        { id: 9, parentId: 0, name: "一级菜单C", rank: 1 },
        { id: 10, parentId: 0, name: "一级菜单end", rank: 1 }
      ]
    }
  },
  components: {
    SearchBar,
    BreedBirth,
    PigTree
  },
  computed: {
    /* 转树形数据 */
    optionData() {
      let cloneData = JSON.parse(JSON.stringify(this.list)); // 对源数据深度克隆
      return cloneData.filter(father => {
        // 循环所有项，并添加children属性
        let branchArr = cloneData.filter(child => father.id == child.parentId); // 返回每一项的子级数组
        branchArr.length > 0 ? (father.children = branchArr) : ""; //给父级添加一个children属性，并赋值
        return father.parentId == 0; //返回第一层
      });
    }
  },
  methods: {
    addHandle (data) {
      this.birthVisible = true
      this.$nextTick(() => {
        for (let key in this.$refs.refBirth.dataForm) {
          if (data && data.hasOwnProperty(key)) {
            if (key == 'boar') {
              this.$refs.refBirth.dataForm[key] = data[key].earmark
            } else {
              this.$refs.refBirth.dataForm[key] = data[key]
            }
          }
        }
        if (!data) {
          delete this.$refs.refBirth.dataForm.id
        } else {
            this.$refs.refBirth.dataForm['id'] = data['id']
          }
        this.$refs.refBirth.init()
      })
    },
    showUpload () {
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.upload.init()
      })
    }
  }
}
</script>
